package cc.leevi.common.httpproxy.handler;

import cc.leevi.common.httpproxy.HttpProxyRequestHead;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.logging.ByteBufFormat;
import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.internal.StringUtil;

import java.nio.charset.StandardCharsets;

public class CustomMessageHandler extends ChannelDuplexHandler {

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
        System.out.println("read complete ......................");

    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        System.out.println("write .............................");
        formatMessage(ctx,msg);
        super.write(ctx, msg, promise);

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        formatMessage(ctx,msg);
        super.channelRead(ctx, msg);
    }

    private void formatMessage(ChannelHandlerContext ctx,Object msg) {
        try {
            if (msg instanceof ByteBuf) {
                String eventName = "READ11111111111111";
                ByteBuf by = Unpooled.copiedBuffer((ByteBuf) msg);
                String chStr = ctx.channel().toString();
                by.capacity();
                int length = by.readableBytes();
                StringBuilder buf;
                if (length == 0) {
                    buf = new StringBuilder(chStr.length() + 1 + "READ" + 4);
                    buf.append(chStr).append(' ').append(eventName).append(": 0B");
//                return buf.toString();
                } else {

                    byte[] bytes = new byte[length];
                    by.readBytes(bytes);
                    buf = new StringBuilder(eventName+"\r\n");
                    buf.append(new String(bytes));
                    System.out.println(eventName+":"+new String(bytes));
//                int outputLength = chStr.length() + 1 + eventName.length() + 2 + 10 + 1;
////                if (this.byteBufFormat == ByteBufFormat.HEX_DUMP) {
//                int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
//                int hexDumpLength = 2 + rows * 80;
//                outputLength += hexDumpLength;
////                }
//
//                buf = new StringBuilder(outputLength);
//                buf.append(chStr).append(' ').append(eventName).append(": ").append(length).append('B');
////                if (this.byteBufFormat == ByteBufFormat.HEX_DUMP) {
//                buf.append(StringUtil.NEWLINE);
//                ByteBufUtil.appendPrettyHexDump(buf, by);
////                }
////                return buf.toString();
                }
                System.out.println(buf);
            } else if (msg instanceof HttpProxyRequestHead) {
                HttpProxyRequestHead head = (HttpProxyRequestHead) msg;
                if(head.getByteBuf() != null) {
                    final ByteBuf bf = Unpooled.copiedBuffer(head.getByteBuf());
                    System.out.println("HEAD............:"+bf.toString(StandardCharsets.UTF_8));
                }
            } else {
                System.out.println("MSG................"+ msg);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
